package com.ruoyi.web.controller.medicine;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.medicine.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @program: RuoYi-Vue-master
 * @ClassName CartController
 * @description:
 * @author: peterZcx
 * @create: 2024-09-01 14:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/medicine/cart")
public class CartController extends BaseController {
    @Autowired
    private ICartService cartService;
    //我的采购。查询购物车中的采购信息

    //删除采购项
    @DeleteMapping("/{drugIds}")
    public AjaxResult remove(@PathVariable Long[] drugIds) throws JsonProcessingException {
        Long userId = getUserId();
        return toAjax(cartService.deleteCartByDrugIds(userId,drugIds));
    }
    //更新采购数量
    @PutMapping("/{drugId}/{cartDrugNum}")
    public AjaxResult updateNum(@PathVariable Long drugId,@PathVariable Long cartDrugNum) throws JsonProcessingException {
        Long userId = getUserId();
        return toAjax(cartService.updateCartNumService(userId,drugId,cartDrugNum));
    }

    @GetMapping("/list")
    public AjaxResult myCart() throws JsonProcessingException {
        Long userId = getUserId();
        return success(cartService.myCartListService(userId));
    }

    /**
     * 加入购物车
     */
    @PreAuthorize("@ss.hasPermi('medicine:cart:add')")
    @Log(title = "购物车信息", businessType = BusinessType.DELETE)
    @PostMapping("/{drugIds}")
    public AjaxResult add(@PathVariable Long[] drugIds) throws JsonProcessingException {
        Long userId = getUserId();

        return toAjax(cartService.addCart(userId, drugIds));
    }

}
